<?php

/**
 * This is the model class for table "member".
 *
 * The followings are the available columns in table 'member':
 * @property string $cmnd
 * @property string $fullname
 * @property integer $sex
 * @property string $birthday
 * @property string $birthplace
 * @property string $phone
 * @property string $cellphone
 * @property string $email
 * @property string $address
 * @property string $contact_address
 * @property string $avatar
 * @property integer $user_id
 * @property string $social_position
 * @property string $social_activity
 *
 * The followings are the available model relations:
 * @property Users $user
 * @property AnnouncementAssign[] $assigns
 */
class Member extends MemberBase
{
	public $announcement_id;
	public $course_name;
	public $scholarship;
	public $code;
	public $purpose_join;
	public $event_joined;
	public $view_fg;
	public $invite;
	public $comment;

	/**
	 * Returns the static model of the specified AR class.
	 * @return Member the static model class
	 */
	public static function model($className=__CLASS__)
	{
		return parent::model($className);
	}

	/**
	 * @return string the associated database table name
	 */
	public function tableName()
	{
		return 'member';
	}

	/**
	 * @return array validation rules for model attributes.
	 */
	public function rules()
	{
		// NOTE: you should only define rules for those attributes that
		// will receive user inputs.
		return array(
			array('cmnd, fullname, birthday, birthplace, address, avatar', 'required'),
			array('sex, user_id', 'numerical', 'integerOnly'=>true),
			array('cmnd', 'length', 'max'=>12),
			array('fullname, email', 'length', 'max'=>128),
			array('birthday', 'length', 'max'=>10),
			array('birthplace, address, contact_address, avatar', 'length', 'max'=>255),
			array('phone, cellphone', 'length', 'max'=>20),
			array('social_position, social_activity', 'safe'),
			// The following rule is used by search().
			// Please remove those attributes that should not be searched.
			array('fullname, course_name, scholarship, purpose_join, event_joined, view_fg, code', 'safe', 'on'=>'search'),
		);
	}

	/**
	 * @return array relational rules.
	 */
	public function relations()
	{
		// NOTE: you may need to adjust the relation name and the related
		// class name for the relations automatically generated below.
		return array(
			'memberCode' => array(self::HAS_ONE, 'MemberCode', 'user_id',
							'on' => 'memberCode.active = 1',
							'with' => 'course',
							'joinType' => 'INNER JOIN'),
			'user' => array(self::BELONGS_TO, 'Users', 'user_id', 'joinType' => 'INNER JOIN'),
			'assigns' => array(self::HAS_ONE, 'AnnouncementAssign', '',
								'on' => 'assigns.member_id = t.user_id and assigns.announcement_id = :announcement',
								'params' => array(':announcement' => $this->announcement_id),
								'joinType' => 'LEFT JOIN'),
			/*'views' => array(self::HAS_ONE, 'AnnouncementView', '',
								'on' => 'views.user_id = t.user_id and views.announcement_id = :announcement',
								'params' => array(':announcement' => $this->announcement_id),
								'joinType' => 'LEFT JOIN'),*/
		);
	}

	/**
	 * @return array customized attribute labels (name=>label)
	 */
	public function attributeLabels()
	{
		return array(
			'cmnd'	=> 'Chứng minh nhân dân',
			'fullname' => 'Thành viên',
			'sex' => 'Giới tính',
			'birthday' => 'Ngày sinh',
			'birthplace' => 'Nơi sinh',
			'phone' => 'Điện thoại',
			'cellphone' => 'Di động',
			'email' => 'Thư điện tử',
			'address' => 'Địa chỉ thường trú',
			'contact_address' => 'Địa chỉ liên lạc',
			'avatar' => 'Hình ảnh',
			'social_position' => 'Các chức vụ Đoàn/Hội/CLB đã từng nắm giữ (nếu có)',
			'social_activity' => 'Các hoạt động xã hội',

			'invite' => 'Mời',

			// From relation
			'course_name' => 'Khóa',
			'scholarship' => 'Học bổng',
			'purpose_join' => 'Dự kiến tham dự',
			'event_joined' => 'Tham dự',
			'view_fg' => 'Xem',
			'code' => 'Mã SV',
			'comment' => 'Lý do không tham dự',
		);
	}

	/**
	 * Retrieves a list of models based on the current search/filter conditions.
	 * @return CActiveDataProvider the data provider that can return the models based on the search/filter conditions.
	 */
	public function search($print = false)
	{
		// Warning: Please modify the following code to remove attributes that
		// should not be searched.

		$criteria=new CDbCriteria;

		$criteria->with = array('memberCode' => array('select'=> 'memberCode.status, memberCode.code'),
								'assigns' => array('params' => array(':announcement' => $this->announcement_id)));

		// From table member
		$criteria->compare('fullname',$this->fullname,true);
		$criteria->compare('assigns.view_fg',$this->view_fg);

		// Search field in relation
		$criteria->compare('course.id', $this->course_name);
		$criteria->compare('memberCode.status', $this->scholarship);
		$criteria->compare('memberCode.code', $this->code, true);

		if ($this->purpose_join == "0") {
			$criteria->addCondition('(purpose_join = 0 or purpose_join is null)');
		}
		else {
			$criteria->compare('purpose_join', $this->purpose_join, true);
		}
		if ($this->event_joined == "0") {
			$criteria->addCondition('(event_joined = 0 or event_joined is null)');
		}
		else {
			$criteria->compare('event_joined', $this->event_joined, true);
		}

		if ($this->invite == "0") {
			$criteria->addCondition('assigns.member_id is null');
		}
		elseif ($this->invite == "1") {
			$criteria->addCondition('assigns.member_id is not null');
		}
		$criteria->compare('assigns.comment', $this->comment, true);

		$criteria->together = true;

		return new CActiveDataProvider($this, array(
			'criteria'=>$criteria,
			'pagination'=>false,
			'sort' =>array (
                'defaultOrder' => array('course.id' => 'desc', 'fullname' => 'asc'),
				'attributes' => array(
					'course_name' => array(
						'asc'=>'course.id ASC',
						'desc'=>'course.id DESC',
					),
					'scholarship' => array(
						'asc'=>'memberCode.status ASC',
						'desc'=>'memberCode.status DESC',
					),
					'purpose_join' => array(
						'asc'=>'purpose_join ASC',
						'desc'=>'purpose_join DESC',
					),
					'event_joined' => array(
						'asc'=>'event_joined ASC',
						'desc'=>'event_joined DESC',
					),
					'code' => array(
						'asc'=>'code ASC',
						'desc'=>'code DESC',
					),
					'invite' => array(
						'asc'=>'assigns.id ASC',
						'desc'=>'assigns.id DESC',
					),
					/*'view' => array(
						'asc'=>'views.id ASC',
						'desc'=>'views.id DESC',
					),*/
					'comment' => array(
						'asc'=>'assigns.comment ASC',
						'desc'=>'assigns.comment DESC',
					),
					'fullname',
				),
			)
		));
		
	}
}